package com.alai04.android.scoreboard;

import android.content.Context;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class Prefs extends PreferenceActivity {
	// Option names and default values
	private static final String OPT_NAME_PREFIX = "name";
	private static final String OPT_PN4 = "players";
	private static final String OPT_BID = "times";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);

		ListPreference prefName;
		Preference.OnPreferenceChangeListener onPrefChg = new Preference.OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference,
					Object newValue) {
				preference.setSummary(newValue.toString());
				return true;
			}
		};
		for (int i = 1; i <= 4; i++) {
			prefName = (ListPreference) findPreference(OPT_NAME_PREFIX + i);
			prefName.setSummary(getName(getApplicationContext(), i));
			prefName.setOnPreferenceChangeListener(onPrefChg);
			prefName.setEntries(Scoreboard.contactNames);
			prefName.setEntryValues(Scoreboard.contactNames);
		}
	}

	public static boolean getPN4(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getBoolean(OPT_PN4, false);
	}

	public static boolean getBID(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getBoolean(OPT_BID, false);
	}

	public static String getName(Context context, int n) {
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getString(OPT_NAME_PREFIX + n,
						context.getString(R.string.name_default));
	}
}